﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MossmanMill.DataModel;
using MossmanMill.View;
using MossmanMill.VisualDataRep;
using MossmanMill.RackPlanner;

namespace MossmanMill
{
    public partial class MainForm : Form
    {

        public MainForm()
        {
            InitializeComponent();
            Init();
            MouseWheel += new MouseEventHandler(ScrollWheel);
        }

        private void DataEntryButton_Click(object sender, EventArgs e)
        {
            //this.Visible = false;
            var dataInputForm = new DataInputForm();
            dataInputForm.ShowDialog();
        }

        private void Inventory_Click(object sender, EventArgs e)
        {
            var inventory = new InventoryManager();
            inventory.ShowDialog();
        }

        private void importToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ImportForm importForm = new ImportForm(this);
            importForm.Show();
        }

        private void reportToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ReportForm reportForm = new ReportForm();
            reportForm.Show();
        }

        private void ScrollWheel(object sender, MouseEventArgs e)
        {
            var rect = new Rectangle(mapRect.Left - 40, mapRect.Bottom + 30, mapRect.Width + 40, 162);
            var point = new Point(Cursor.Position.X - this.Location.X - viewPanel.Location.X, Cursor.Position.Y - this.Location.Y - viewPanel.Location.Y);
            if (level == Level.ROOM && rect.Contains(point))
            {
                scrollbar.Move((e.Delta/-6));
                viewPanel.Invalidate(rect);
            }
        }

        private void LookupClick(object sender, EventArgs e)
        {
            var lookup = new LookupForm(this);
            lookup.Show();
        }

        private void RackPlannerClick(object sender, EventArgs e)
        {
            var rackPlanner = new RackPlanner.RackPlanner(this);
            rackPlanner.Show();
        }

        private void FreeSpaceSearchClick(object sender, EventArgs e)
        {
            var freeSpaceSearch = new SearchForm(this);
            freeSpaceSearch.Show();
        }

        private void EditInDragDrop(object sender, EventArgs e)
        {
            var rackPlanner = new RackPlanner.RackPlanner(this);
            switch (level)
            {
                case Level.RACK:
                    if (selectedRack == null) break;
                    rackPlanner.LoadRack(selectedRack);
                    break;
                case Level.CARD:
                    if (selectedCard == null) break;
                    if (selectedCard.SelectedSensor != null) rackPlanner.LoadSensor(selectedCard.SelectedSensor);
                    else rackPlanner.LoadCard(selectedCard);
                    break;

            }
            rackPlanner.Show();
        }

        private void EditInManualForm(object sender, EventArgs e)
        {
            var dataInput = new DataInputForm();
            switch (level)
            {
                case Level.RACK:
                    if (selectedRack == null) break;
                    dataInput.LoadRack(selectedRack);
                    break;
                case Level.CARD:
                    if (selectedCard == null) break;
                    if (selectedCard.SelectedSensor != null) dataInput.LoadSensor(selectedCard.SelectedSensor);
                    else dataInput.LoadCard(selectedCard);
                    break;

            }
            dataInput.Show();
        }

        private void Exit(object sender, EventArgs e)
        {
            Application.Exit();   
        }

        public void LoadCard(Card card)
        {
            selectedRoom = card.Rack.Room;
            selectedRack = card.Rack;
            selectedCard = card;
            level = Level.CARD;
            initCard(card);
            Invalidate();
        }

        public void LoadRack(Rack rack)
        {
            selectedRoom = rack.Room;
            selectedRack = rack;
            level = Level.RACK;
            initRack(rack);
            Invalidate();
        }
    }
}
